package com.dajie.pt.controller.invitation;

import com.dajie.common.util.Base64;
import com.dajie.pt.dao.DBconn;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dajie.pt.util.MD5Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"/invitation"})
public class InvitationController {
    private static Logger logger = LoggerFactory.getLogger(InvitationController.class);

    public InvitationController() {
    }

    @RequestMapping({"unsubscribe_edm"})
    public ModelAndView unsubscribeEdm(HttpServletRequest request, HttpServletResponse response, @RequestParam(
            value = "m_to_uid",
            required = true
    ) String uid, @RequestParam(
            value = "to_email",
            required = false
    ) String toEmail, @RequestParam(
            value = "m_category",
            required = true
    ) String category, @RequestParam(
            value = "code",
            required = true
    ) String code) {
        logger.info("InvitationController --> unsubscribeEdm into uid:{} toEmail:{} category:{} code:{}", new Object[]{uid, toEmail, category, code});
        if (!"".equals(uid) && !"".equals(category) && !"".equals(code)) {
            HashMap map = new HashMap();
            map.put("m_to_uid", uid);
            map.put("m_category", category);
            String originCode = Base64.encode(map.toString());
            String md5Code = MD5Util.getMD5Code(map.toString());
            logger.info("InvitationController --> unsubscribeEdm contrast code:{} originCode:{}", code, originCode);
            logger.info("InvitationController --> unsubscribeEdm contrast code:{} md5Code:{}", code, md5Code);
            if (!originCode.equals(code)) {
                return new ModelAndView("common/500");
            } else {
                String categoryName = "该";
                String isUnsub = "退订成功";
                if (!uid.isEmpty() && category != null) {
                    int mv = DBconn.writeUnsub(uid, toEmail);
                    logger.info("InvitationController --> unsubscribeEdm update db count:{}", Integer.valueOf(mv));
                }

                ModelAndView mv1 = null;
                String userAgent = request.getHeader("User-Agent");
                if (!userAgent.contains("Android") && !userAgent.contains("iPhone") && !userAgent.contains("Windows Phone")) {
                    mv1 = new ModelAndView("invitation/unsubscribe_edm");
                } else {
                    mv1 = new ModelAndView("invitation/unsubscribe_edm_mobile");
                }

                mv1.addObject("categoryName", categoryName);
                mv1.addObject("isUnsub", isUnsub);
                return mv1;
            }
        } else {
            return new ModelAndView("common/500");
        }
    }
}
